﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace smartphone.App_Code
{
    public class News
    {
        string title; //tieu de tin tuc
        string content; // noi dung tin tuc
        DateTime postDate; //ngay nhap tin

        public News()
        {

        }

        public string Title
        {
            get
            {
                return title;
            }
        }

        public string Content
        {
            get
            {
                return content;
            }
        }

        public DateTime PostDate
        {
            get
            {
                return postDate;
            }
        }


        /// <summary>
        /// Lay tin tuc cho slideshow o Footer
        /// </summary>
        /// <returns></returns>
        public DataTable GetNews()
        {
            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "GetNews";

            DataTable table = GenericDataAccess.ExecuteSelectCommand(com);
            return table;
        }

        /// <summary>
        /// Lay chi tiet tin tuc
        /// </summary>
        /// <param name="NewsID">ma tin</param>
        /// <returns></returns>
        public News GetNewsDetail(int NewsID)
        {
            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "GetNewsDetail";

            DbParameter param = com.CreateParameter();
            param.ParameterName = "@NewsId";
            param.Value = NewsID;
            param.DbType = DbType.Int16;

            com.Parameters.Add(param);

            DataTable table = GenericDataAccess.ExecuteSelectCommand(com);
            News n = new News();

            if (table.Rows.Count > 0)
            {
                DataRow row = table.Rows[0];
                n.title = row["tieuDe"].ToString();
                n.content = row["noiDung"].ToString();
                n.postDate = (DateTime)row["ngayPost"];
            }

            return n;
        }


        /// <summary>
        /// lay 5 tin khac voi tin dang xem
        /// </summary>
        /// <param name="NewsID"></param>
        /// <returns></returns>
        public DataTable GetOthersNews(int NewsID)
        {

            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "GetOthersNews";

            DbParameter param = com.CreateParameter();
            param.ParameterName = "@NewsId";
            param.Value = NewsID;
            param.DbType = DbType.Int32;

            com.Parameters.Add(param);
            DataTable table = GenericDataAccess.ExecuteSelectCommand(com);
            return table;
        }

        public DataTable GetAllNews()
        {
            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "GetAllNews";
            DataTable table = GenericDataAccess.ExecuteSelectCommand(com);
            return table;
        }


        /// <summary>
        /// Lay tat cac tin tuc cho admin quan li
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllNewsAdmin()
        {
            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "GetAllNewsAdmin";
            DataTable table = GenericDataAccess.ExecuteSelectCommand(com);
            return table;
        }


        /// <summary>
        /// Xoa tin tuc
        /// </summary>
        /// <param name="newsID"></param>
        /// <returns></returns>
        public bool DeleteNews(int newsID)
        {
            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "DeleteNews";

            DbParameter param = com.CreateParameter();
            param.ParameterName = "@maTin";
            param.Value = newsID;
            param.DbType = DbType.Int32;
            com.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(com);

            return (result != -1);
            
        }

        /// <summary>
        /// Tao tin tuc moi
        /// </summary>
        /// <param name="tieuDe"></param>
        /// <param name="noiDung"></param>
        /// <param name="ngayPost"></param>
        /// <param name="ngayCapNhat"></param>
        /// <returns></returns>
        public bool CreateNews(string tieuDe, string noiDung)
        {
            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "CreateNews";

            DbParameter param = com.CreateParameter();
            param.ParameterName = "@tieuDe";
            param.Value = tieuDe;
            param.DbType = DbType.String;
            com.Parameters.Add(param);

            param = com.CreateParameter();
            param.ParameterName = "@noiDung";
            param.Value = noiDung;
            param.DbType = DbType.String;
            com.Parameters.Add(param);

            param = com.CreateParameter();
            param.ParameterName = "@ngayPost";
            param.Value = DateTime.Now;
            param.DbType = DbType.DateTime;
            com.Parameters.Add(param);

            param = com.CreateParameter();
            param.ParameterName = "@ngayCapNhat";
            param.Value = DateTime.Now;
            param.DbType = DbType.DateTime;
            com.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(com);

            return (result != -1);
        }


        /// <summary>
        /// Cap nhat tin tuc
        /// </summary>
        /// <param name="maTin"></param>
        /// <param name="tieuDe"></param>
        /// <param name="noiDung"></param>
        /// <param name="ngayCapNhat"></param>
        /// <returns></returns>
        public bool UpdateNews(int maTin, string tieuDe, string noiDung)
        {

            DbCommand com = GenericDataAccess.CreateCommand();
            com.CommandText = "UpdateNews";

            DbParameter param = com.CreateParameter();
            param.ParameterName = "@tieuDe";
            param.Value = tieuDe;
            param.DbType = DbType.String;
            com.Parameters.Add(param);

            param = com.CreateParameter();
            param.ParameterName = "@noiDung";
            param.Value = noiDung;
            param.DbType = DbType.String;
            com.Parameters.Add(param);

            param = com.CreateParameter();
            param.ParameterName = "@maTin";
            param.Value = maTin;
            param.DbType = DbType.Int32;
            com.Parameters.Add(param);

            param = com.CreateParameter();
            param.ParameterName = "@ngayCapNhat";
            param.Value = DateTime.Now;
            param.DbType = DbType.DateTime;
            com.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(com);

            return (result != -1);
        }
    }
}